﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;

public partial class Reports_BI_KPI_CashLastMonth_2_Cube : System.Web.UI.Page
{

    #region Members

    StringBuilder sbTbl = new StringBuilder();
    string nl = @"
";
    DateTime dt = DateTime.Now;
    DataTable tblData_1;
    DataTable tblData_2;

    int TblCurRow = -1;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ret = null;

        oCaption.Text = "Ежедневная выручка ; Сегодня - " + String.Format("{0:00} / ", dt.Month) + dt.Year;

        ret = MakeData();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        ret = MakeHTML_Table();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        MakeHTML_Table();
    }

    private Exception MakeData()
    {
        Exception ret = null;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);
        MakeDataTable();
        ret = FillDataTable();
        if (ret != null) return ret;
        return ret;
    }

    Exception FillDataTable()
    {

        Exception ret = null;
        DataTable tblSrc = new DataTable("tbl");
        Exception err = new Ko2.DB.BI.Reports.Cash().GetCubeCheck(ref tblSrc);
        if (err != null)
        {
            ret = new Exception(err.Message, err);
            return ret;
        }
        foreach (DataRow r in tblSrc.Rows)
        {
            DataRow newRow = tblData_1.NewRow();
            newRow[0] = "";
            if (r[0] != DBNull.Value) newRow[0] = r[0];
            newRow[1] = 0;
            if (r[1] != DBNull.Value) newRow[1] = r[1];
            newRow[2] = 0;
            if (r[2] != DBNull.Value) newRow[2] = r[2];
            newRow[3] = 0;
            if (r[3] != DBNull.Value) newRow[3] = r[3];
            tblData_1.Rows.Add(newRow);
        }
        DataTable tblSrc2 = new DataTable("tbl");
        err = new Ko2.DB.BI.Reports.Cash().GetCubeTrade(ref tblSrc2);
        if (err != null)
        {
            ret = new Exception(err.Message, err);
            return ret;
        }
        foreach (DataRow r in tblSrc2.Rows)
        {
            DataRow newRow = tblData_2.NewRow();
            newRow[0] = "";
            if (r[0] != DBNull.Value) newRow[0] = r[0];
            newRow[1] = 0;
            if (r[1] != DBNull.Value) newRow[1] = r[1];
            newRow[2] = 0;
            if (r[2] != DBNull.Value) newRow[2] = r[2];
            tblData_2.Rows.Add(newRow);
        }
        return ret;
    }

    private void MakeDataTable()
    {
        tblData_1 = new DataTable("Chart_1");
        tblData_1.Columns.Add("Дата", typeof(string));
        tblData_1.Columns.Add("Оборот", typeof(Decimal));
        tblData_1.Columns.Add("Кол-во маг. в ДП", typeof(int));
        tblData_1.Columns.Add("Кол-во чеков", typeof(int));

        tblData_2 = new DataTable("Chart_2");
        tblData_2.Columns.Add("Дата", typeof(string));
        tblData_2.Columns.Add("Оборот", typeof(Decimal));
        tblData_2.Columns.Add("Кол-во маг. в ДП", typeof(int));        
    }

    Exception MakeHTML_Table()
    {
        Exception ret = null;
        TblCurRow = -1;
        sbTbl.Append(
@"
<script language='javascript'>
//<![CDATA[
    function drawTable_1() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Дата');
        data.addColumn('number', 'Кол-во чеков');
        data.addColumn('number', 'Оборот');
        data.addColumn('number', 'Кол-во маг. в ДП');
        data.addRows(" + tblData_1.Rows.Count + @"); 
");

        foreach (DataRow r in tblData_1.Rows)
            TblAddRow(Convert.ToString(r[0]), Convert.ToInt32(r[1]), Convert.ToDecimal(r[2]), Convert.ToInt32(r[3]));

        sbTbl.Append(@"
        var table = new google.visualization.Table(document.getElementById('tbl_div_1'));
        table.draw(data, {showRowNumber: false, sort: 'disable'});
    }
");

        TblCurRow = -1;
        sbTbl.Append(
@"
    function drawTable_2() {
        var data2 = new google.visualization.DataTable();
        data2.addColumn('string', 'Дата');
        data2.addColumn('number', 'Оборот');
        data2.addColumn('number', 'Кол-во маг. в ДП');
        data2.addRows(" + tblData_2.Rows.Count + @"); 
");

        foreach (DataRow r in tblData_2.Rows)
            TblAddRow2(Convert.ToString(r[0]), Convert.ToDecimal(r[1]), Convert.ToInt32(r[2]));

        sbTbl.Append(@"
        var table2 = new google.visualization.Table(document.getElementById('tbl_div_2'));
        table2.draw(data2, {showRowNumber: false, sort: 'disable'});
");

        sbTbl.Append(@"
    }
//]]>
</script>");


        oBody.Text = sbTbl.ToString();
        return ret;
    }

    Exception TblAddRow(string name, int val1, Decimal val2, int val3)
    {
        Exception ret = null;
        TblCurRow++;
        sbTbl.Append(@"
data.setCell(" + TblCurRow + @", 0, '" + name + @"');" + nl);

        string str = val1.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 1, " + val1 + @", '" + str + @"');
");

        str = val2.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 2, " + val2 + @", '" + str + @"');
");

        str = val3.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data.setCell(" + TblCurRow + @", 3, " + val3 + @", '" + str + @"');
");


        return ret;
    }

    Exception TblAddRow2(string name, Decimal val2, int val3)
    {
        Exception ret = null;
        TblCurRow++;
        sbTbl.Append(@"
data2.setCell(" + TblCurRow + @", 0, '" + name + @"');" + nl);

        string str = val2.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data2.setCell(" + TblCurRow + @", 1, " + ((int)val2).ToString("G") + @", '" + str + @"');
");

        str = val3.ToString("C"); str = str.Replace("р.", "");
        if (str.Length > 3) str = str.Substring(0, str.Length - 3);
        sbTbl.Append(@"data2.setCell(" + TblCurRow + @", 2, " + val3 + @", '" + str + @"');
");


        return ret;
    }

}